<template>
	<view>
		<web-view :src="surl" bindload="loaded"></web-view>

<!-- 		<u-button type='primary' :plain="true"
			style="position:fixed ;right: 3vh;bottom: 20vh;z-index: 999;width: 100px;">地图加载中
		</u-button> -->

	</view>
</template>

<script>
	import {
		getlinelist,
		getfilebyoutid,
		deleteline,
		searchline,
	} from '@/common/api.js'
	import coordtransform from 'coordtransform'
	import {
		baseUrl
	} from '@/common/config.js'
	import {
		ref
	} from 'vue'

	export default {
		data() {
			return {
				baseurl: `https://project.gxrunxiang.com/map.html?temp=sttx`,
				surl: `https://project.gxrunxiang.com/map.html?temp=ttxx2sddxx`,
				// surl: `https://project.gxrunxiang.com/index.html#/maintenance/line`,
				screenHeight: getApp().globalData.screenHeight,
				loading: false,
				islocated: false,
				seachlist: [],
				keyword: '',
				outdata: [],
				isselect: false,
				outisimage: false,
				outcenter: [108.1, 23],
				ismapready: false,
				showpop: false,
				urls: [],
				userinfo: {},
				show: false,
				showconfirm: false,
				actionslist: [{
						name: '百度导航',
					},
					{
						name: '腾讯导航',
					},
					{
						name: '高德导航',
					}
				],
				showsheet: false,
				items: [],
				token: '',
			}
		},
		onShow() {
			// plus.screen.lockOrientation('landscape-primary');
			// plus.navigator.setFullscreen(true)
		},
		onLoad(option) {
			if(option.latlon)
			{
				this.gomap(option.latlon)
			}
			this.outlocation()
			// this.onSend("getdata",'')
			// uni.showToast({
			// 	title: '获取地址失败，将导致部分功能不可用',
			// 	icon: 'none'
			// });
			// setTimeout(()=>{this.outlocation()},5000)
			// this.outlocation()
			// if (window)
			// 	window.addEventListener("message", this.message, false);
		},
		onUnload() {
			// plus.screen.lockOrientation('portrait-primary')
			// plus.navigator.setFullscreen(false)
		},
		onShow() {
			// if (this.ismapready)
			// this.getdata()
			// this.senddata()

		},

		methods: {
			gomap(latlon) {
				
					let latitude = parseFloat(latlon.split(',')[0])
					let longitude = parseFloat(latlon.split(',')[1])
					var lola=coordtransform.wgs84togcj02(longitude,latitude)
					uni.openLocation({
						latitude: lola[1] ,
						longitude: lola[0],
						success: function() {
							console.log('success');
						}
					});
				
			},
			loaded(e) {
				console.log(e.detail)
			},
			// message: function(res) {
			// 	console.log(res)


			// 	var data

			// 	if (window) {
			// 		if (res.data.type != "WEB_INVOKE_APPSERVICE")
			// 			return
			// 		data = res.data.data.arg

			// 	} else
			// 		data = res.detail.data[0];
			// 	var action = data.action
			// 	// console.log(action)
			// 	if (action == "selectit") {
			// 		uni.navigateTo({
			// 			url: `point?latlng=${data.para.lat+","+data.para.lng}`
			// 		})
			// this.showsheet=true
			// var itemList = ["百度导航", "腾讯导航", "高德导航"];
			// uni.showActionSheet({
			//     itemList: itemList,
			//     success:  (res)=>
			//     {
			//       var index = res.tapIndex;
			//       var tapItem = itemList[index];
			//       // console.log("点击了 " + tapItem);
			// 	  if (tapItem == "百度导航")
			// 	  	this.wakeBaidu()
			// 	  if (tapItem == "高德导航")
			// 	  	this.wakeGaoDe()
			// 	  if (tapItem == "腾讯导航")
			// 	  	this.wakeTengXun()
			//     },
			// });
			// this.outlocation()

			// }
			// if (action == "calllocate") {
			// 	this.outlocation()
			// }			
			// if (action == "calldata") {
			// 	this.getdata()
			// }
			// },
			outlocation() {

				// 				uni.getLocation({
				// 					type: 'wgs84',
				// 					geocode: true, //设置该参数为true可直接获取经纬度及城市信息
				// 					success: (res) => {

				// 						this.outcenter = [res.longitude, res.latitude]
				// 						this.islocated = true

				// this.onSend("located",res.longitude + "，" + res.latitude)



				// 					},
				// 					fail: () => {
				// 						uni.showToast({
				// 							title: '获取地址失败，将导致部分功能不可用',
				// 							icon: 'none'
				// 						});
				// 					}
				// 				});
				// wx.getFuzzyLocation({
				// 	type: 'wgs84',
				// 	success(res) {
				// 		const latitude = res.latitude
				// 		const longitude = res.longitude
				// 		console.log("located"+res)
				// 		this.onSend("located", res.longitude + ',' + res.latitude)
				// 	}
				// })
				//获得地理定位信息
				// this.onSend("located",   '108.3669,22.81673' )
				uni.getLocation({
					type: 'wgs84',
					success: (resp) => {
						// console.log(11111);
						//保存纬度数据
						let latitude = resp.latitude;
						//保存经度度数据
						let longitude = resp.longitude;
						console.log('经度' + latitude);
						console.log('纬度' + longitude);
						this.token = uni.getStorageSync("$token")
						this.sendcmd(`located(${resp.longitude},${resp.latitude});setToken('${this.token}')`)
					},
					fail: function(resp) {
						console.log(resp);
					}
				});

			},
			popon(e) {
				// console.log("e")
				// console.log(e)

				// this.urls = []
				this.loading = true
				var tempitem = []
				for (var i = 0; i < e.length; i++) {
					e[i].graphic.attributes.urls = []
					if (e[i].graphic.attributes.ImageList) {
						e[i].graphic.attributes.ImageList.forEach((element) => {
							e[i].graphic.attributes.urls.push(`${baseUrl}/SysFile/download?id=${element.Id}`)
						});
						tempitem.push(e[i].graphic.attributes)
					}
				}

				// this.item = e
				this.items = tempitem
				this.showpop = true;
				this.loading = false;
			},
			outselect(i) {
				// let i = this.view.extent.center.latitude + "," + this.view.extent.center.longitude
				uni.navigateTo({
					url: `point?latlng=${i}`
				})
			},
			selectnav(Latlng) {
				this.showsheet = true;
				this.showpop = false;
				this.TagX = Latlng.split(',')[1]
				this.TagY = Latlng.split(',')[0]
			},
			nav(e) {
				if (e.name == "百度导航")
					this.wakeBaidu()
				if (e.name == "高德导航")
					this.wakeGaoDe()
				if (e.name == "腾讯导航")
					this.wakeTengXun()

			},
			wakeTengXun() {
				var purl = " https://apis.map.qq.com/uri/v1/routeplan?type=drive&fromcoord=" + 0 + "," + 0 +
					"&to=目的地&tocoord=" + this.TagY + "," + this.TagX + "&policy=0&referer=外业举证app&coord_type=1"
				console.log(purl)
				plus.runtime.openURL(purl, function(e) {
					plus.nativeUI.alert("本机未安装指定的地图应用");
				});
			},
			wakeGaoDe() {
				var purl = "http://uri.amap.com/navigation?from=" + 0 + "," + 0 + ",我的位置&to=" + this.TagX + "," + this
					.TagY +
					",目的地&mode=car&policy=1&src=外业举证&coordinate=wgs84&callnative=1"
				plus.runtime.openURL(purl, function(e) {
					plus.nativeUI.alert("本机未安装指定的地图应用");
				});
			},
			wakeBaidu() {

				// urlObject 是我这边地址栏查询参数对象
				var queryStr = '?origin=name:我的位置|latlng:' + 0 + ',' + 0 + '&destination=' + this.TagY + ',' + this.TagX +
					'&region=目的地&coord_type=wgs84&mode=driving';


				var scheme = 'bdapp://map/direction' + queryStr;


				var purl = 'http://api.map.baidu.com/direction' + queryStr + '&output=html';
				console.log(purl)
				plus.runtime.openURL(purl, function(e) {
					plus.nativeUI.alert("本机未安装指定的地图应用");
				});

			},
			search() {
				searchline(this.keyword).then(res => {
					this.seachlist = res
				})
			},
			getdata() {
				let para = {
					Parameters: [],
					orderBys: ["Idx"]
				}
				getlinelist(para).then(res => {
					this.outdata = res.data;
					// console.log(this.outdata)
					// this.showdatas(this.datas)
					// this.senddata(this.outdata)
				})
			},
			del(id) {
				// let p = [this.item.Id]
				let p = [id]
				deleteline(p).then(res => {
					this.getdata()
					this.showconfirm = false
					this.showpop = false

				})
			},
			mapready() {
				this.ismapready = true
			},
			goline(latlng) {
				var i = [parseFloat(latlng.split(',')[1]), parseFloat(latlng.split(',')[0])]
				this.outcenter = i
			},
			evalJs: function() {
				this.$refs.webview.evalJs("alert('aaaaaa')");
			},
			sendcmd(cmd) {

				// const vw = this.$scope.$getAppWebview().children()[0]
				// vw.evalJS(`located(${this.outcenter[0]},${this.outcenter[1]},'${uni.getStorageSync("$token")}')`)
				// // console.log(vw.evalJS)
				// console.log("token"+uni.getStorageSync("$token"))
				this.surl = `${this.baseurl}#${cmd}`

			},
			senddata(p) {
				const vw = this.$scope.$getAppWebview().children()[0]
				// console.log(vw.evalJS)
				// vw.evalJS(`showdatas(${JSON.stringify(p)})`)
				// var a =JSON.stringify(p)
				// uni.setStorageSync("linedata",a)
				vw.evalJS(`getdata()`)
			}

		}
	}
</script>

<style>

</style>
